<?php

/**
 * This is the model class for table "log_login".
 *
 * The followings are the available columns in table 'log_login':
 * @property string $id_log_login
 * @property string $id_user
 * @property string $status
 * @property string $fail_reason
 * @property string $time_date
 * @property integer $ip_long
 */
class LogLogin extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return LogLogin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'log_login';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ip_long', 'numerical', 'integerOnly'=>true),
			array('id_user, status', 'length', 'max'=>10),
			array('fail_reason', 'length', 'max'=>200),
			array('time_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_log_login, id_user, status, fail_reason, time_date, ip_long', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'User' => array(self::HAS_ONE, 'User', array('id_user' => 'id_user'))
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_log_login' => 'Id Log Login',
			'id_user' => 'Id User',
			'status' => 'Status',
			'fail_reason' => 'Fail Reason',
			'time_date' => 'Time',
			'ip_long' => 'Ip Long',
		);
	}
	
	public function beforeValidate()
	{
		if(!empty($this->ip_long)){
			if(!is_int($this->ip_long)){
				$this->ip_long = ip2long($this->ip_long);
			}
		}
		return parent::beforeValidate();
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_log_login',$this->id_log_login,true);
		$criteria->compare('id_user',$this->id_user,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('fail_reason',$this->fail_reason,true);
		$criteria->compare('time_date',$this->time_date,true);
		$criteria->compare('ip_long',$this->ip_long);

		$criteria->with = array('User');
		
		$postData = app()->request->getParam('LoginActivitySearchForm', null);
		if($postData){
			if(!empty($postData['conditionVal'])){
				$conditionVal = $postData['conditionVal'];
//				$conditionVal = str_replace('-', '', $conditionVal);
				$conditionVal = str_replace('  ', ' ', $conditionVal);
				$conditionVal = trim($conditionVal);
				
				if($postData['conditionField'] == 'username'){
					$criteria->addSearchCondition('User.username', $conditionVal);
				}else if($postData['conditionField'] == 'ip_long'){
					$criteria->addSearchCondition('inet_ntoa(t.ip_long)', $conditionVal);
				}else if($postData['conditionField'] == 'status'){
					$criteria->addSearchCondition("CONCAT(t.status, ' - ', t.fail_reason)", $conditionVal);
//					$criteria->addSearchCondition("t.fail_reason", $conditionVal, true, 'OR');
				}
			}
			if(!empty($postData['dateFrom'])){
				$startDate = date('Y-m-d 00:00:00', strtotime($postData['dateFrom']));
				$criteria->addCondition("t.time_date>='{$startDate}'");
			}
			if(!empty($postData['dateTo'])){
				$endDate = date('Y-m-d 23:59:59', strtotime($postData['dateTo']));
				$criteria->addCondition("t.time_date<='{$endDate}'");
			}
		}
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort' => array(
				'defaultOrder'=>'time_date DESC',
				'attributes' => array(
					'time_sort' => array(
						'asc' => 't.time_date ',
						'desc' => 't.time_date DESC',
					)
				)
			),
			'pagination' => array(
				'pageSize'=> app()->user->getData()->getIndividualConfig()->getPagination()
			)
		));
	}
	
	public function __get($key)
	{
		$action = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
		if(method_exists($this, $action)){
			return $this->$action();
		}
		
		return parent::__get($key);
	}
	public function getIpPlain()
	{
		return long2ip($this->ip_long);
	}
	
	public function purge($beforeTime)
	{
		if(is_int($beforeTime)){
			$beforeTime = date('Y-m-d H:i:s', $beforeTime);
		}
		
		$criteria = new CDbCriteria;
		$criteria->addCondition("time_date<'{$beforeTime}'");
		
		return $this->deleteAll($criteria);
	}
}